package com.lg.algorithm.leetcode;

import java.util.HashSet;

/**
 * 给定一个字符串 s ，请你找出其中不含有重复字符的 最长子串 的长度。
 */
public class LongestSubstring {

    public static void main(String[] args) {
        String s = "abcabcbb";
        System.out.println(lengthOfLongestSubstring(s));
    }

    public static  int lengthOfLongestSubstring(String s) {
        int maxLength = 0;
        int post = 0;
        int index = -1;
        char[] chars = s.toCharArray();
        String tempStr = "";
        while (post < chars.length) {
            if ((index = tempStr.indexOf(String.valueOf(chars[post]))) > -1) {
                maxLength = maxLength > tempStr.length() ? maxLength : tempStr.length();
                tempStr = tempStr.length() > (index + 1) ? tempStr.substring(index + 1) : "";
                tempStr = tempStr + chars[post];
            } else {
                tempStr = tempStr + chars[post];
            }
            post++;
        }
        maxLength = maxLength > tempStr.length() ? maxLength : tempStr.length();
        return maxLength;
    }
}
